local yanzheng = fk.CreateSkill{
  name = "mini__yanzhengs",
}

Fk:loadTranslationTable{
  ["mini__yanzhengs"] = "言正",
  [":mini__yanzhengs"] = "摸牌阶段，你可以改为与一名其他角色各摸两张牌，然后直到你的下回合开始，" ..
      "你或其下次造成伤害后，你弃置伤害来源区域内的一张牌。",

  ["#mini__yanzhengs-ask"] = "你可发动 言正，改为与一名其他角色各摸两张牌，<br/>然后直到你的下回合开始，" ..
      "你或其下次造成伤害后，你弃置伤害来源区域内的一张牌",
  ["#mini__yanzhengs-discard"] = "言正：弃置%dest一张牌",
  ["@mini__yanzhengs_oversee"] = "言正",
}

yanzheng:addEffect(fk.EventPhaseStart, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self.name) and player.phase == Player.Draw and not data.phase_end and
      #player.room:getOtherPlayers(player) > 0
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = player.room:getOtherPlayers(player)
    local result = room:askToChoosePlayers(player, {
      targets = targets,
      min_num = 1,
      max_num = 1,
      prompt = "#mini__yanzhengs-ask",
      skill_name = self.name,
    })
    if #result > 0 then
      event:setCostData(self, {tos = result})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    data.phase_end = true
    local to = event:getCostData(self).tos[1] ---@type ServerPlayer
    player:drawCards(2, self.name)
    if to:isAlive() then to:drawCards(2, self.name) end
    if player:isAlive() then
      room:addTableMark(player, "@mini__yanzhengs_oversee", to.general)
      room:addTableMark(player, "_mini__yanzhengs_oversee", to.id)
    end
  end
})

yanzheng:addEffect(fk.Damage, {
  anim_type = "control",
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    local markedTargets = player:getTableMark("_mini__yanzhengs_oversee")
    return #markedTargets > 0 and 
           target and target:isAlive() and player:isAlive() and
           (target == player or table.contains(markedTargets, target.id)) and 
           not target:isAllNude() and
           player:getMark("_mini__yanzhengs_triggered_"..target.id) == 0
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards
    if target == player then
      cards = table.filter(player:getCardIds("hej"), function (id)
        return not player:prohibitDiscard(id)
      end)
      if #cards == 0 then return end
      cards = room:askToCards(player, {
        min_num = 1,
        max_num = 1,
        include_equip = true,
        skill_name = self.name,
        pattern = tostring(Exppattern{ id = cards }),
        cancelable = false,
        prompt = "#mini__yanzhengs-discard::" .. target.id,
        expand_pile = player:getCardIds("j"),
      })
    else
      cards = room:askToChooseCard(player, {
        target = target,
        flag = "hej",
        skill_name = self.name,
        prompt = "#mini__yanzhengs-discard::" .. target.id
      })
    end
    room:throwCard(cards, self.name, target, player)
    room:setPlayerMark(player, "_mini__yanzhengs_triggered_"..target.id, 1)
  end
})

local yanzhengClear = function (player)
  local room = player.room
  local markedTargets = player:getTableMark("_mini__yanzhengs_oversee")
  room:setPlayerMark(player, "@mini__yanzhengs_oversee", 0)
  room:setPlayerMark(player, "_mini__yanzhengs_oversee", 0)
  for _, targetId in ipairs(markedTargets) do
    room:setPlayerMark(player, "_mini__yanzhengs_triggered_"..targetId, 0)
  end
end
yanzheng:addEffect(fk.TurnStart, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:getMark("_mini__yanzhengs_oversee") ~= 0
  end,
  on_refresh = function (self, event, target, player, data)
    yanzhengClear(player)
  end
})

yanzheng:addLoseEffect(function (self, player, is_death)
  yanzhengClear(player)
end)

return yanzheng
